<?php

header( 'Content-Type:text/html;charset=utf-8 ');
require './Admin/Model/VideoModel.php';
require 'Controller.php';

class VideoController extends Controller
{

    function listvideos()
    {
        $model = new VideoModel();
        $data = $model->getVideoLIst();

        $this->assign('videos', $data);
        $this->display();

    }

    function delvideopic($vid)
    {
        $model = new VideoModel();
        $data['videos'] = $model->getVideo($vid);
        $filename = $data['videos']['pic'];
        $filepath = "./photos/" . $filename;
        if (file_exists($filepath)) {
            unlink($filepath);
        }
    }

    function delvideo()
    {
        $vid = $_GET['vid'];
        $this->delvideopic($vid);
        $model = new VideoModel();
        $result = $model->delVideo($vid);
        if ($result) {
            echo "删除成功！3秒后返回";
            header("refresh:3;url='admin.php?c=video&m=listvideos");
        } else {
            echo "删除失败！3秒后返回";
            header("refresh:3;url='admin.php?c=video&m=listvideos");
        }
    }

    function regvideo()
    {
        $model= new VideoModel();
        $datatype = $model->getTypeList();
        $this->assign('videos', $datatype);
        $this->display();
    }

    function addvideo()
    {
        $videoname = trim($_POST['vname']);
        $videodegree = $_POST['videodegree'];  
        $gender=$_POST['gender'];
        $birthdate=$_POST['birthdate'];
        $graduatedate=$_POST['graduatedate'];
        $hobby=implode($_POST['hobby'],',');
        $school=$_POST['school'];
        $intro = $_POST['vintro'];
        $pic = $_FILES['img'];
        $adress = $_POST['vadress'];
        $uploadtime = date('y-m-d h:i:s', time());
        $uploadadmin = 2;
        $hittimes = 0;
        $downtimes = 0;
        $model = new VideoModel();
        //echo $videoname, $videodegree, $gender, $birthdate, $graduatedate,$hobby, $school, $intro, $pic;
        $result =  $model->regVideo($videoname, $videodegree, $gender, $birthdate, $graduatedate,$hobby, $school, $intro, $pic,$adress, $uploadtime, $uploadadmin, $hittimes, $downtimes);
        if ($result) {
            echo "插入成功！3秒后返回";
            header("refresh:3;url='admin.php?c=video&m=listvideos");
        } else {
            echo "插入失败！3秒后返回";
            header("refresh:3;url='admin.php?c=video&m=listvideos");
        }
    }

    function editvideo(){
        $vid = $_GET['vid'];
        $model = new VideoModel();
        $data = $model->getVideo($vid);
        $data['videos']=$data;
        $tid= $data['videos']['tid'];
        $datatname = $model->getVideoType($tid);
        $datatype = $model->getTypeList();
        $this->assign('tname',$datatname);
        $this->assign('videos', $data);
        $this->assign('types',$datatype);
        $this->display();


    }
    function updvideo(){
        $vid=$_POST['vid'];
        $videoname = trim($_POST['vname']);
        $videodegree = $_POST['videodegree'];
        $gender=$_POST['gender'];
        $birthdate=$_POST['birthdate'];
        $graduatedate=$_POST['graduatedate'];
        $hobby=implode($_POST['hobby'],',');
        $school=$_POST['school'];
        $intro = $_POST['vintro'];
        $pic = $_FILES['img'];
        $model = new VideoModel();
        $result= $model->editVideo($vid, $videoname, $videodegree, $gender, $birthdate, $graduatedate,$hobby, $school,$intro,$pic);
        if ($result) {
            echo "修改成功！3秒后返回";
            header("refresh:3;url='admin.php?c=video&m=listvideos");
        } else {
            echo "修改失败！3秒后返回";
            header("refresh:3;url='admin.php?c=video&m=listvideos");
        }
    }

}
?>